package com.elitel.frame.web.serverinfocustom.service.impl;

import com.elitel.common.utils.SysUserUtils;
import com.elitel.frame.web.cfgtableextend.dao.CfgTableExtendDao;
import com.elitel.frame.web.cfgtableextend.entity.CfgTableExtend;
import com.elitel.frame.web.cfgtablequerysql.dao.CfgTableQuerySqlDao;
import com.elitel.frame.web.cfgtablequerysql.entity.CfgTableQuerySql;
import com.elitel.frame.web.serverinfo.dto.ServerConfigDTO;
import com.elitel.frame.web.serverinfo.dto.ServerInfoDTO;
import com.elitel.frame.web.serverinfo.dto.ServerQueryFieldDTO;
import com.elitel.frame.web.serverinfocustom.dto.SingleServerInfoDTO;
import com.elitel.frame.web.serverinfocustom.service.SererInfoCustomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author m
 */
@Service
public class SingleServerInfoService implements SererInfoCustomService {


    @Resource
    private CfgTableExtendDao cfgTableExtendDao;

    @Resource
    private CfgTableQuerySqlDao cfgTableQuerySqlDao;

    @Override
    public void save(ServerInfoDTO dto) {
        SingleServerInfoDTO singleServerInfoDTO = dto.getSuccessor(SingleServerInfoDTO.class);
        ServerConfigDTO basicConfig = singleServerInfoDTO.getBasicConfig();
        // 设置cfg_extend
        CfgTableExtend cfgTableExtend = new CfgTableExtend();
        cfgTableExtend.setGuid(basicConfig.getServiceCode());
        cfgTableExtend.setServicename(basicConfig.getServerName());
        cfgTableExtend.setDtConfigId(singleServerInfoDTO.getTableId());
        cfgTableExtend.setRemark(basicConfig.getRemark());
        cfgTableExtend.setDbKey(basicConfig.getDbKey());
        cfgTableExtend.setSyscatch(0);
        cfgTableExtend.setCatchtime(0);
        cfgTableExtend.setIspagination(Integer.parseInt(basicConfig.getPaginationFlag() == null ? "0" : basicConfig.getPaginationFlag()));
        cfgTableExtend.setTimeDelay(0);
        cfgTableExtend.setResponsemode(0);

        // 保存cfg_extend
        String serverCode = cfgTableExtend.getGuid();
        CfgTableExtend dbData = cfgTableExtendDao.queryById(serverCode);
        if (dbData == null) {
            cfgTableExtend.setCreatetime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            this.cfgTableExtendDao.insert(cfgTableExtend);
        } else {
            this.cfgTableExtendDao.update(cfgTableExtend);
        }


        CfgTableQuerySql cfgTableQuerySql = new CfgTableQuerySql();
        cfgTableQuerySql.setGuid(serverCode);
        cfgTableQuerySql.setServiceid(serverCode);
        cfgTableQuerySql.setServicetype("1");
        cfgTableQuerySql.setSqlcontent(this.getWhereSql(dto));
        cfgTableQuerySql.setRemark("");
        // 保存 base_querysql
        cfgTableQuerySqlDao.deleteByServerCode(serverCode);
        cfgTableQuerySql.setCreatedate(LocalDateTime.now());
        cfgTableQuerySql.setCreateuser(SysUserUtils.getLoginAccount());
        this.cfgTableQuerySqlDao.insert(cfgTableQuerySql);
    }

    @Override
    public Map<String, Object> selectByServerId(String serverId) {
        CfgTableExtend cfgTableExtend = cfgTableExtendDao.queryById(serverId);
        String dtConfigId = cfgTableExtend.getDtConfigId();
        HashMap<String, Object> res = new HashMap<>();
        res.put("tableId", dtConfigId);
        return res;
    }

    private String getWhereSql(ServerInfoDTO dto) {
        StringBuilder whereSql = new StringBuilder();
        // 获取查询字段
        List<ServerQueryFieldDTO> queryFieldList = dto.getQueryFieldList();
        for (ServerQueryFieldDTO serverQueryFieldDTO : queryFieldList) {
            String fieldCode = serverQueryFieldDTO.getFieldCode();
            String metaFieldCode = serverQueryFieldDTO.getMetaFieldCode();
            String fieldCondition = serverQueryFieldDTO.getFieldCondition();
            //(1-动态参数查询，2-固定参数查询)
            Integer fieldSearchType = serverQueryFieldDTO.getFieldSearchType();
            String fieldDefaultValue = serverQueryFieldDTO.getFieldDefaultValue();

            whereSql.append("<#if ").append(fieldCode).append("?? && ").append(fieldCode).append("!=''>");
            if (fieldSearchType == 1) {
                whereSql.append(" and ").append(metaFieldCode).append(" ").append(fieldCondition).append(" '${").append(fieldCode).append("}'");
            } else {
                whereSql.append(" and ").append(metaFieldCode).append(" ").append(fieldCondition).append(" ").append(fieldDefaultValue);
            }
            whereSql.append("</#if>");
        }
        return whereSql.toString();
    }
}
